/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.main;

import cn.ac.iie.di.wx.mq.common.RuntimeEnv;
import static cn.ac.iie.di.wx.mq.common.RuntimeEnv.redisVos;
import cn.ac.iie.di.wx.mq.httpservice.HttpSender;
import static cn.ac.iie.di.wx.mq.main.QmCplMain.init;
import cn.ac.iie.di.wx.mq.redis.RedisThread;
import cn.ac.iie.di.wx.mq.utils.RedisUtil;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 * 筛选出符合要求的日志 输出到指定文件
 *
 * @author Administrator
 */
public class Qm15CplMain {

    static Logger logger = Logger.getLogger(Qm15CplMain.class.getName());

    public static void main(String[] args) throws FileNotFoundException, IOException {
        //读取日志文件，筛选出
        //获取根路径
        try {
            init();
            String root = System.getProperty("user.dir");
            String absolutePath = root + "/" + (String) (RuntimeEnv.getParam(RuntimeEnv.sourceFileName));//筛选出OCRThemes的日志
            String WritePath = root + "/" + (String) (RuntimeEnv.getParam(RuntimeEnv.targetFileName));//筛选出OCRThemes的日志
            logger.info("从源文件" + absolutePath + ",筛选出2017-08-29 15:00:00以后的日志，写入" + WritePath);
            FileReader reader = new FileReader(absolutePath);
            BufferedReader br = new BufferedReader(reader);
            FileWriter writer = new FileWriter(WritePath);
            BufferedWriter bw = new BufferedWriter(writer);
            String str = "";
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date compareDate = df.parse((String) RuntimeEnv.getParam(RuntimeEnv.starttime));
            Date compareDate_1 = df.parse((String) RuntimeEnv.getParam(RuntimeEnv.endtime));
            logger.info("筛选时间范围起于："+(String) RuntimeEnv.getParam(RuntimeEnv.starttime)+",止于"+(String) RuntimeEnv.getParam(RuntimeEnv.endtime));
            while ((str = br.readLine()) != null) {
                Thread.sleep(10);//一秒钟发一个。。。
//                String request = str.split(",")[1];
                //重新发送
                
                String time = str.split(",")[0];
                logger.info(time);
                Date logDate = df.parse(time);
//                logger.info(logDate.after(compareDate));
                if (logDate.after(compareDate) && logDate.before(compareDate_1)) {
                    logger.info("符合时间范围条件的日志内容为:" + str);
                    bw.write(str);
                    bw.newLine();
                }

            }
            bw.flush();
            bw.close();
            writer.close();
            br.close();
            reader.close();
            //截取关键字段，判断是否是异常返回，如果是，则调用补全接口重新发送一次。
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}
